package com.drop.service.impl;

import com.drop.common.EasyUIDataGridResult;
import com.drop.common.MyCart;
import com.drop.common.MyOrder;
import com.drop.common.States;
import com.drop.dao.CommonDao;
import com.drop.dao.GoodsDao;
import com.drop.dao.OrderDao;
import com.drop.entity.Childorder;
import com.drop.entity.Goods;
import com.drop.entity.Order;
import com.drop.service.OrderService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 陈东一 on 2017/9/24 15:34
 */
@Service
@Transactional
public class OrderServiceImpl implements OrderService {
	private Log log = LogFactory.getLog(this.getClass());

	@Autowired
	private OrderDao orderDao;

	@Autowired
	private CommonDao commonDao;

	@Autowired
	private GoodsDao goodsDao;

	@Override
	public int addOrder(int userId,String[] goodsId,String[] isselect,String[] quantity) throws Exception{
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		//所有商品总价
		float total = 0.0f ,temp = 0.0f;
		//商品id列表
		List<String> ids = new ArrayList<>();
		List<String> num = new ArrayList<>();
 		for(int i=0; i<goodsId.length; i++){
			if ("1".equals(isselect[i])){
				ids.add(goodsId[i]);
				num.add(quantity[i]);
			}
		}
		List<Goods> goods = goodsDao.getGoodsByIds(ids);
		//单件商品总价列表
		List<Float> moneys = new ArrayList<>();
		for(int i=0; i<goods.size(); i++){
			//单间商品总价
			temp = goods.get(i).getPrice() * Float.parseFloat(num.get(i));
			moneys.add(temp);
			total = total + temp;
		}
//		log.info("总价格: "+total);
		orderDao.insertOrder(userId,total,1,sdf.format(d), States.待付款.getValue());
		Order o = orderDao.getOrder(userId,sdf.format(d));
		int id = o.getOrderId();
		orderDao.insertChildOrder(id,ids,moneys,num);
			return id;
	}

	@Override
	public Order updateOrder(int id, int state, String address) throws Exception {
		orderDao.updateState(id, String.valueOf(state),address);
		return (Order) commonDao.getById(Order.class,id);
	}

	@Override
	public Order showOrder(int id) throws Exception {
		Order order = (Order) commonDao.getById(Order.class, id);
		return order;
	}

	@Override
	public EasyUIDataGridResult showAllOrder(int shipid, String page, String rows) {
		EasyUIDataGridResult e = new EasyUIDataGridResult();
		List<Order> orders = (List<Order>)(List)commonDao.getByPage(Order.class,Integer.parseInt(page),Integer.parseInt(rows));
		List<MyOrder> orders1 = new ArrayList<>();
		Set<Childorder> childorders = new HashSet<>();
		e.setTotal(commonDao.count(Order.class));
		StringBuilder goods;
		for (Order o : orders){
			goods = new StringBuilder();
			childorders = o.getChildorders();
			for (Childorder c : childorders) {
				goods.append(c.getGoods().getGoodsName()+"-"+ c.getMoney()+"元" +"-"+ c.getQuantity()+"件,");
			}
			orders1.add(new MyOrder(o.getOrderId(), o.getState().getStatus(), o.getUser().getUserName(), o.getUser().getTel(), o.getSumMoney(), o.getOrderTime(), o.getAddress(), goods.toString()));
		}
		e.setRows(orders1);
		return e;
	}

	@Override
	public MyCart updateOrder(String ids, int state) throws Exception {
		orderDao.updateState(ids,state);
		return new MyCart("true","发货成功");
	}

	@Override
	public MyCart updateOrder(String id, String address) throws Exception {
		Order order = (Order) commonDao.getById(Order.class, Integer.valueOf(id));
		order.setAddress(address);
		commonDao.update(order);
		return new MyCart("true","修改地址成功");
	}

	@Override
	public MyCart deleteOrder(String orderId) {
		Order order = (Order) commonDao.getById(Order.class, Integer.valueOf(orderId));
		commonDao.delete(order);
		return new MyCart("true","取消订单成功");
	}
}
